package com.siyu.rehearsal.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.siyu.basedata.plan.domain.Plan;
import com.siyu.basedata.plan.service.IPlanService;
import com.siyu.common.core.controller.BaseController;
import com.siyu.common.core.domain.AjaxResult;
import com.siyu.rehearsal.dto.PlanContrastDto;
import com.siyu.rehearsal.dto.PlanRatioDto;
import com.siyu.rehearsal.previewpresult.domain.PreviewPResult;
import com.siyu.rehearsal.previewpresult.service.IPreviewPResultService;
import com.siyu.rehearsal.previewsresult.domain.PreviewSresult;
import com.siyu.rehearsal.previewsresult.service.IPreviewSresultService;
import com.siyu.rehearsal.service.PlanDispatchService;
import com.siyu.rehearsal.vo.PlanRatioDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/rehearsal/contrast")
public class PlanContrastController extends BaseController {

    @Autowired
    private PlanDispatchService planDispatchService;

    @Autowired
    private IPlanService planService;

    @Autowired
    private IPreviewSresultService previewSresultService;

    @Autowired
    private IPreviewPResultService previewPResultService;

    /**
     * 可对比调度方案列表查询
     * @param planContrastDto
     * @return
     */
    @PostMapping("/plan/list")
    public AjaxResult getPlan(@RequestBody PlanContrastDto planContrastDto){
        QueryWrapper<Plan> planQueryWrapper = new QueryWrapper<>();
        planQueryWrapper.select("id","plan_name")
                .like(BeanUtil.isNotEmpty(planContrastDto.getPlanName()),"plan_name",planContrastDto.getPlanName())
                .eq(BeanUtil.isNotEmpty(planContrastDto.getCalcType()),"calc_type",planContrastDto.getCalcType())
                .ge(BeanUtil.isNotEmpty(planContrastDto.getStartTime()),"start_time",planContrastDto.getStartTime())
                .le(BeanUtil.isNotEmpty(planContrastDto.getEndTime()),"end_time",planContrastDto.getEndTime())
                .eq("pre_type","预演")
                .eq("rscd",planContrastDto.getRscd())
                .orderByDesc("create_time");
        List<Plan> list = planService.list(planQueryWrapper);
        return AjaxResult.success(list);
    }

    /**
     * 实时方案对比
     * @param planRatioDto
     * @return
     */
    @PostMapping("/plan/ratio")
    public AjaxResult getRatioData(@RequestBody PlanRatioDto planRatioDto){
        HashMap<String, Object> planRatioDataVos = planDispatchService.getRatioData(planRatioDto);
        return AjaxResult.success(planRatioDataVos);
    }

    /**
     * 预演方案对比可视化结果查询
     * @param id
     * @return
     */
    @GetMapping("/plan/compare")
    public AjaxResult getRatioResultData(@RequestParam("id")String id){
        QueryWrapper<PreviewSresult> previewSresultQueryWrapper = new QueryWrapper<>();
        previewSresultQueryWrapper.select("geojson","max_fa","rsvr","fv","i_populace","aal","ah","f_storage")
                .eq("plan_id",id);
        PreviewSresult previewSresult = previewSresultService.getOne(previewSresultQueryWrapper);
        return AjaxResult.success(previewSresult);
    }

    /**
     * 调度建议查询
     * @param id
     * @return
     */
    @GetMapping("/plan/propose")
    public AjaxResult getRatioPropose(@RequestParam("id") String id) {
        QueryWrapper<PreviewPResult> previewPResultQueryWrapper = new QueryWrapper<>();
        previewPResultQueryWrapper.select("d_advice")
                .eq("plan_id",id);
        PreviewPResult previewPResult = previewPResultService.getOne(previewPResultQueryWrapper);
        return AjaxResult.success(previewPResult);
    }
}

